package com.leigang.java.code.batch.config;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @program: yaner-boot
 * @description: 决策器练习
 * @author: leigang
 * @create: 2020-01-02 23:34
 */
@Configuration
@EnableBatchProcessing
public class DeciderDemo {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Step stepDeciderOne(){
        return stepBuilderFactory.get("stepDeciderOne").tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("stepDeciderOne");
                return RepeatStatus.FINISHED;
            }
        }).build();
    }

    @Bean
    public Step stepDeciderTwo(){
        return stepBuilderFactory.get("stepDeciderTwo").tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("stepDeciderTwo");
                return RepeatStatus.FINISHED;
            }
        }).build();
    }

    @Bean
    public Step stepDeciderThree(){
        return stepBuilderFactory.get("stepDeciderThree").tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("stepDeciderThree");
                return RepeatStatus.FINISHED;
            }
        }).build();
    }

    @Bean
    public Step stepDeciderFour(){
        return stepBuilderFactory.get("stepDeciderFour").tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("stepDeciderFour");
                return RepeatStatus.FINISHED;
            }
        }).build();
    }

    /**
     * 创建决策器
     * @return
     */
    @Bean
    public JobExecutionDecider myDecider(){
        return new MyDecider();
    }

    @Bean
    public Job deciderDemoJob(){
        return jobBuilderFactory.get("deciderDemoJob")
                .start(stepDeciderOne())
                .next(myDecider())
                .from(myDecider()).on("even").to(stepDeciderTwo())
                .from(myDecider()).on("odd").to(stepDeciderThree())
                .from(stepDeciderThree()).on("*").to(myDecider())
                .end()
                .build();
    }
}
